<?php

namespace x_mier\auth;

use think\facade\Route;

class Service extends \think\Service
{
    use \x_mier\Jump;
    public function boot(Route $route)
    {
        $this->registerRoutes(function () {
            $verification = 'system';
            if (!empty(config('app.app_map'))) {
                if (!empty($verifications = array_flip(config('app.app_map'))['system'])) {
                    $verification = $verifications;
                };
            }
            $url = request()->url();
            if (!empty($url)) {
                $url = mb_ereg_replace('^(/| )+', '', $url);
                if (explode('/', $url)[0] == $verification) {
                    Route::group('Login', function () use ($verification) {
                        Route::get('index', function () use ($verification) {
                            $session = session('auth_user');
                            if (isset($session)) {
                                return $this->success('检测到你已经登录，正在跳转', '/' . $verification);
                            }
                            app("view")->engine()->layout(false);
                            return view("index/login", ['verification' => $verification]);
                        });
                        Route::post('index', '/')->middleware(\x_mier\auth\middleware\Auth::class, 'login');
                        Route::rule('logout', '')->middleware(\x_mier\auth\middleware\Auth::class, ['logout', "login/index"]);
                    });
                }
            }
        });
    }
}
